<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

class Event extends Model
{
    public $timestamps = false;
    protected $fillable = ['name','type','permission','address','date','client_id'];

    public function getDateAttribute(){
        return Carbon::parse($this->attributes['date'])->format('Y-m-d');
    }
    public function getPhotoAttribute(){
        return config('app.url').$this->attributes['photo'];
    }

    public function accepted(){
        foreach ($this->employees as $employee){
            if ($employee->pivot->response !='Accept') return false;
        }
        $this->status = 'Accepted';
        $this->save();
    }

    public function employees(){
        return $this->belongsToMany('App\User','App\UserEvent')
            ->withPivot('response');
    }

    public function messages(){
        return $this->hasMany('App\Message');
    }

    public function client(){
        return $this->belongsTo('App\Client');
    }
}
